﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Demo.Net.Infrastructure
{
    public static class SecurityHelper
    {
        private const string key_128 = "!010GMBZERO00010!";

        public static string MD5Create(string input)
        {
            return MD5Create(Encoding.UTF8.GetBytes(input));
        }

        public static string MD5WithSalt(string input, string salt)
        {
            return MD5Create(MD5Create(input) + salt);
        }

        public static string MD5Create(byte[] input)
        {
            using MD5 mD = MD5.Create();
            byte[] array = mD.ComputeHash(input);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.AppendFormat("{0:x2}", array[i]);
            }

            return stringBuilder.ToString();
        }

        public static string SHA1Create(string input)
        {
            return SHA1Create(Encoding.UTF8.GetBytes(input));
        }

        public static string SHA1WithSalt(string input, string salt)
        {
            return SHA1Create(SHA1Create(input) + salt);
        }

        public static string SHA1Create(byte[] input)
        {
            using SHA1 sHA = SHA1.Create();
            byte[] array = sHA.ComputeHash(input);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.AppendFormat("{0:x2}", array[i]);
            }

            return stringBuilder.ToString();
        }

        public static string AESEncrypt(string input, string key = "!010GMBZERO00010!")
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            using Aes aes = Aes.Create();
            using ICryptoTransform transform = aes.CreateEncryptor(bytes, aes.IV);
            using MemoryStream memoryStream = new MemoryStream();
            using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            using (StreamWriter streamWriter = new StreamWriter(stream))
            {
                streamWriter.Write(input);
            }

            byte[] iV = aes.IV;
            byte[] array = memoryStream.ToArray();
            byte[] array2 = new byte[iV.Length + array.Length];
            Buffer.BlockCopy(iV, 0, array2, 0, iV.Length);
            Buffer.BlockCopy(array, 0, array2, iV.Length, array.Length);
            return Convert.ToBase64String(array2);
        }

        public static string AESDecrypt(string input, string key = "!010GMBZERO00010!")
        {
            byte[] array = Convert.FromBase64String(input);
            byte[] array2 = new byte[16];
            byte[] array3 = new byte[array.Length - array2.Length];
            Buffer.BlockCopy(array, 0, array2, 0, array2.Length);
            Buffer.BlockCopy(array, array2.Length, array3, 0, array.Length - array2.Length);
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            using Aes aes = Aes.Create();
            using ICryptoTransform transform = aes.CreateDecryptor(bytes, array2);
            using MemoryStream stream = new MemoryStream(array3);
            using CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
            using StreamReader streamReader = new StreamReader(stream2);
            return streamReader.ReadToEnd();
        }
    }
}
